package com.test.io;

import java.io.*;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class IORead implements Delayed {


    public static void main(String[] args) throws Exception {

        String filePath = "E:/file.txt"; // 替换为你的文件路径


        //大批量写入
        File file=new File(filePath);
        if(file.exists()){
            file.delete();
            file.createNewFile();
        }
        FileWriter fileWriter=new FileWriter(file);

        for(int i=0;i<300000000;i++){
             fileWriter.write(String.valueOf(i));
        }

//        long lineCount = 0;
//        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
//            String line;
//            while ((line = br.readLine()) != null) {
//                // 处理文件行，例如打印或计数
//                // System.out.println(line);
//                lineCount++;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("Total lines: " + lineCount);


//        long time1= System.currentTimeMillis();
//        FileInputStream fileInputStream = new FileInputStream(filePath);
//        //BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//        FileOutputStream fileOutputStream = new FileOutputStream("E:/filefile.txt");
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while (fileInputStream.read(bytes) != -1) {
//            fileOutputStream.write(bytes, 0, len);
//        }
//        System.out.println(System.currentTimeMillis()-time1);


//        testMethod2();

    }


    //从一个文件读取数据，然后写入到另一个文件中；相当于内容的复制
    public static void testMethod2() {

        File fileIN = new File("E:/file.txt"); //定义输入文件
        File fileOUT = new File("E:/filefile.txt"); //定义输出文件

        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {

            fis = new FileInputStream(fileIN); //输入流连接到输入文件
            fos = new FileOutputStream(fileOUT); //输出流连接到输出文件

            byte[] arr = new byte[10]; //该数组用来存入从输入文件中读取到的数据
            int len; //变量len用来存储每次读取数据后的返回值

            while ((len = fis.read(arr)) != -1) {
                fos.write(arr, 0, len);
            }//while循环：每次从输入文件读取数据后，都写入到输出文件中

        } catch (IOException e) {
            e.printStackTrace();
        }

        //关闭流
        try {
            fis.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return 0;
    }

    @Override
    public int compareTo(Delayed o) {
        return 0;
    }
}
